package com.example.javascaffold.utils;


import com.alibaba.fastjson.serializer.SerializerFeature;

import java.util.Map;

import static com.alibaba.fastjson.JSON.parseObject;
import static com.alibaba.fastjson.JSON.toJSONString;


/**
 * <p><strong>JSON转换工具类</strong></p>
 * <p>
 *     将JSON string转换为Object <br>
 *     将Null转换为"" <br>
 * </p>
 * <li>
 * <p>
 *     <strong>示例1: JSON 转 Object</strong> <br>
 *     School school = ConvertUtils.parse(params, School.class); <br>
 * </p>
 * </li>
 * <li>
 * <p>
 *     <strong>示例2: Null 转 ""</strong> <br>
 *     将返回结果的null转Empty (返回null前端不好处理) <br>
 *     Object objectResult = ConvertUtils.nullToEmptyJson(object); <br>
 * </p>
 * </li>
 * @author song
 * @see com.alibaba.fastjson.JSON
 * @see Map
 * @see String
 * @since 10:27 2021/8/12
 **/
@SuppressWarnings("unchecked")
public class ConvertUtils {

    private ConvertUtils() {}

    /**
     * <p>
     *     <strong>示例1: JSON 转 Object</strong> <br>
     *     School school = ConvertUtils.parse(params, School.class); <br>
     * </p>
     * @param params 传入的Map
     * @param clazz 对象的class
     * @return T 处理的结果
     * @author song
     * @since 14:36 2021/8/12
    **/
    public static <T> T parse(Map<String, Object> params, Class<?> clazz) {
        return (T) parseObject(toJSONString(params), clazz);
    }

    /**
     * <p>
     *     <strong>示例2: Null 转 ""</strong> <br>
     *     将返回结果的null转Empty (返回null前端不好处理) <br>
     *     Object objectResult = ConvertUtils.nullToEmptyJson(object); <br>
     * </p>
     * @param object 传入的对象
     * @return Object 处理的结果
     * @author song
     * @since 8:12 2021/8/17
    **/
    public static Object nullToEmptyJson(Object object) {
        String returnString = toJSONString(object,
                // json格式输出
                SerializerFeature.PrettyFormat,
                // 保留map为空的字段
                SerializerFeature.WriteMapNullValue,
                // 将String类型的null转成""形式
                SerializerFeature.WriteNullStringAsEmpty,
                // 将Number类型的null转成0
                SerializerFeature.WriteNullNumberAsZero,
                // 将List类型的null转成[],而不是""
                SerializerFeature.WriteNullListAsEmpty,
                // Boolean类型的null转成false
                SerializerFeature.WriteNullBooleanAsFalse,
                // 处理可能循环引用的问题
                SerializerFeature.DisableCircularReferenceDetect);
        return parseObject(returnString.replace("\n", "").replace("\t", ""));
    }

    /**
     * <p>
     *     <strong>示例3: Null 转 ""</strong> <br>
     *     将返回结果的null转Empty (返回null前端不好处理) <br>
     *     RetPage<?> retPage = ConvertUtils.null2EmptyRetPage(object); <br>
     * </p>
     * @param object 传入的对象
     * @return Object 处理的结果
     * @author song
     * @since 8:12 2021/8/17
     **/
    public static <T> T null2Empty(Object object, Class<?> clazz) {
        String returnString = toJSONString(object,
                // json格式输出
                SerializerFeature.PrettyFormat,
                // 保留map为空的字段
                SerializerFeature.WriteMapNullValue,
                // 将String类型的null转成""形式
                SerializerFeature.WriteNullStringAsEmpty,
                // 将Number类型的null转成0
                SerializerFeature.WriteNullNumberAsZero,
                // 将List类型的null转成[],而不是""
                SerializerFeature.WriteNullListAsEmpty,
                // Boolean类型的null转成false
                SerializerFeature.WriteNullBooleanAsFalse,
                // 处理可能循环引用的问题
                SerializerFeature.DisableCircularReferenceDetect);
        return (T) parseObject(returnString.replace("\n", "").replace("\t", ""), clazz);
    }
}
